package hy.iot.core.support;

import hy.iot.core.entity.IMessage;
import hy.iot.core.utils.ClassUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 协议功能码 实体扫描工具
 * @author lcc
 */
@Slf4j
public class FieldScanClass {
    //自定义类
    private String bodyFidldSchemaPackagesPath;

    private Object headerSchema;

    private Map<String, Class> fidldSchemaMap;

    public FieldScanClass(String FidldSchemaPackagesPath, Object headerSchema) {
        if (headerSchema == null || StringUtils.isBlank(FidldSchemaPackagesPath) || IMessage.class.isAssignableFrom(headerSchema.getClass())){
            throw new NullPointerException("parse schema format error, please check if header schema is valid");
        }
        this.bodyFidldSchemaPackagesPath = FidldSchemaPackagesPath;
        this.headerSchema = headerSchema;
        this.fidldSchemaMap = new HashMap<>();
    }

    public Map<String, Class> getFidldSchemaMap() {
        if (StringUtils.isEmpty(bodyFidldSchemaPackagesPath)) {
            throw new NullPointerException("parse schema format error, please check if body schema is valid");
        }
        List<Class> clsBody = ClassUtils.getInterFaceImplList(bodyFidldSchemaPackagesPath,IMessage.class);
        clsBody.forEach(sc->{
            if (IMessage.class.isAssignableFrom(sc)){
                fidldSchemaMap.put(sc.getSimpleName(),sc);
            }
        });
        return fidldSchemaMap;
    }


}
